package com.peanut.baby.mvp.main.circle;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.peanut.baby.InitManager;
import com.peanut.baby.R;
import com.peanut.baby.model.User;
import com.peanut.devlibrary.adapter.HolderAdapter;
import com.peanut.devlibrary.imageloader.ImageLoader;

import java.util.List;

/**
 * Created by anonymouslystudio on 2017/8/7.
 */

public class FamousUserGridAdapter extends HolderAdapter<User,FamousUserGridAdapter.FamousUserViewHolder>{

    private CircleFamousListener listener;
    public FamousUserGridAdapter(Context context, List<User> listData) {
        super(context, listData);
    }

    public void setCircleFamousListener(CircleFamousListener lis){
        this.listener = lis;
    }

    @Override
    public View buildConvertView(LayoutInflater layoutInflater, User user, int position) {
        return inflate(R.layout.layout_item_famous_grid);
    }

    @Override
    public FamousUserViewHolder buildHolder(View convertView, User user, int position) {
        FamousUserViewHolder holder = new FamousUserViewHolder();
        holder.avatar = (ImageView) convertView.findViewById(R.id.famous_avatar);
        holder.follow = (CheckBox) convertView.findViewById(R.id.famous_follow);
        holder.container = (LinearLayout) convertView.findViewById(R.id.famous_container);

        return holder;
    }


    @Override
    public void bindViewDatas(FamousUserViewHolder holder, final User user, int position) {
        ImageLoader.getInstance().loadImageView(context,user.avatar,holder.avatar);
        holder.follow.setChecked(user.isConcerned == 1);
        holder.follow.setText(user.isConcerned == 1?"已关注":"关注");

        holder.follow.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onFollowStatusChanges(user);
                }
            }
        });

        holder.avatar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(listener != null){
                    listener.onAvatarClicked(user);
                }
            }
        });

        holder.follow.setEnabled(!InitManager.getInstance().getUserId().equals(user.userId+""));
    }

    class FamousUserViewHolder{
        ImageView       avatar;
        CheckBox        follow;
        LinearLayout    container;

    }

    public interface CircleFamousListener{
        void onFollowStatusChanges(User famous);
        void onAvatarClicked(User famous);
    }
}
